<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Ftp{

	var $hostname	= '';
	var $username	= '';
	var $password	= '';
	var $port		= 21;
	var $passive	= true;
	var $conn_id	= false;

	function __construct()
	{		
		$this->hostname = preg_replace( '|.+?://|', '', FTP_HOST );	
		$this->username = FTP_USER;
		$this->password = FTP_PWD;
		$this->port = FTP_PORT;
		$this->connect();
	}
	
	//连接ftp
	function connect()
	{			
		if( ( $this->conn_id = @ftp_connect( $this->hostname, $this->port ) ) === false )
		{
			ERRORLOG && systemlog( 'FTP无法连接！' );	
			return false;
		}
		if( !$this->_login() )
		{
			ERRORLOG && systemlog( 'FTP无法登陆！' );	
			return false;
		}
		//设置被动模式
		if( $this->passive == true )
		{
			ftp_pasv($this->conn_id, true);
		}
		return true;
	}

	//登陆
	function _login()
	{
		return @ftp_login( $this->conn_id, $this->username, $this->password );
	}

	//验证连接ID
	function _is_conn()
	{
		if( !is_resource( $this->conn_id ) )
		{
			ERRORLOG && systemlog( 'FTP无连接！' );
			return false;
		}
		return true;
	}

	//改变目录
	function changedir( $path = '')
	{
		if( $path == '' || !$this->_is_conn() )
		{
			return false;
		}
		$result = @ftp_chdir( $this->conn_id, $path );
		if( $result === false )
		{
			ERRORLOG && systemlog( '无法变更FTP目录！' );	
			return false;		
		}
		return true;
	}
	
	//创建目录
	function mkdir( $path = '', $permissions = NULL )
	{
		//在目录后添加斜杠
		$path = preg_replace( "/(.+?)\/*$/", "\\1/",  FTP_PATH.$path );
		if( $path == '' || !$this->_is_conn() )
		{
			return false;
		}
		$result = @ftp_mkdir( $this->conn_id, $path );
		if( $result === false )
		{
			ERRORLOG && systemlog( '无法在FTP上创建目录' );	
			return false;		
		}
		//设置文件权限
		if( !is_null($permissions))
		{
			$this->chmod( $path, (int)$permissions );
		}
		return true;
	}
	
	//上传文件
	function upload( $locpath, $rempath, $mode = 'auto', $permissions = NULL )
	{
		if( !$this->_is_conn() )
		{
			return false;
		}
		if( !file_exists( $locpath ) )
		{
			ERRORLOG && systemlog( '源文件不存在' );
			return false;
		}
		//设置指定模式
		if( $mode == 'auto' )
		{
			$ext = get_fileext( $locpath );//获取文件后缀
			$mode = $this->_settype( $ext );
		}
		$mode = ( $mode == 'ascii' ) ? FTP_ASCII : FTP_BINARY;
		$result = @ftp_put( $this->conn_id, FTP_PATH.$rempath, $locpath, $mode );
		if( $result === false )
		{
			ERRORLOG && systemlog( '无法在FTP上传文件！' );	
			return false;		
		}
		if( !is_null( $permissions ) )
		{
			$this->chmod( $rempath, (int)$permissions );
		}
		return true;
	}

	//重命名/移动文件
	function rename( $old_file, $new_file, $move = false )
	{
		if( !$this->_is_conn() )
		{
			return false;
		}
		$result = @ftp_rename( $this->conn_id, FTP_PATH.$old_file, FTP_PATH.$new_file );
		if( $result === false )
		{
			$msg = ( $move == false ) ? '无法在FTP上重命名' : '无法在FTP上移动文件';
			ERRORLOG && systemlog($msg);	
			return false;		
		}
		return true;
	}
	
	//移动文件
	function move( $old_file, $new_file )
	{
		return $this->rename( $old_file, $new_file, true );
	}

	//删除文件	
	function delete_file( $filepath )
	{
		if( !$this->_is_conn())
		{
			return false;
		}
		$result = @ftp_delete( $this->conn_id, FTP_PATH.$filepath );
		if( $result === false )
		{
			ERRORLOG && systemlog('无法在FTP上删除文件！');	
			return false;		
		}
		return true;
	}

	//删除目录	
	function delete_dir( $filepath )
	{
		if( !$this->_is_conn())
		{
			return false;
		}
		//在目录后添加斜杠
		$filepath = preg_replace( "/(.+?)\/*$/", "\\1/",  $filepath );
		$list = $this->list_files( $filepath );
		if( $list !== false AND count( $list ) > 0 )
		{
			foreach ( $list as $item )
			{
				if( !@ftp_delete( $this->conn_id, FTP_PATH.$item ) )
				{
					$this->delete_dir( $item );
				}
			}
		}
		$result = @ftp_rmdir( $this->conn_id, FTP_PATH.$filepath );
		if($result === false)
		{
			ERRORLOG && systemlog( '无法在FTP上删除目录！' );
			return false;		
		}
		return true;
	}

	//设置文件权限		
	function chmod($path, $perm)
	{
		if( !$this->_is_conn())
		{
			return false;
		}
		//php version must > 5
		if( !function_exists('ftp_chmod'))
		{
			ERRORLOG && systemlog( '无法通过FTP设置权限！' );	
			return false;		
		}
		$result = @ftp_chmod( $this->conn_id, $perm, $path );
		if($result === false)
		{
			ERRORLOG && systemlog( '无法通过FTP设置权限' );
			return false;		
		}
		return true;
	}

	//文件列表
	function list_files($path = '.')
	{
		if( !$this->_is_conn() )
		{
			return false;
		}
		return ftp_nlist( $this->conn_id, FTP_PATH.$path );
	}

	//FTP映射
	function mirror( $locpath, $rempath )
	{
		if( !$this->_is_conn())
		{
			return false;
		}
		//打开本地目录
		if( $fp = @opendir( $locpath ) )
		{
			//尝试打开远程目录
			if( !$this->changedir( $rempath ) )
			{
				//如果不存在则尝试创建
				if( !$this->mkdir( $rempath ) || !$this->changedir( $rempath ) )
				{
					return false;
				}
			}
			//递归读取本地目录
			while( ( $file = readdir( $fp ) ) !== false )
			{
				if( @is_dir( $locpath.$file ) && substr( $file, 0, 1 ) != '.' )
				{					
					$this->mirror( $locpath.$file."/", $rempath.$file."/" );
				}
				elseif( substr( $file, 0, 1 ) != "." )
				{
					$ext = get_fileext( $file );
					$mode = $this->_settype( $ext );
					$this->upload( $locpath.$file, $rempath.$file, $mode );
				}
			}
			return true;
		}
		return false;
	}

	//设置上传类型
	function _settype($ext)
	{
		$text_types = array( 'txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html',  'phtml', 'shtml', 'log', 'xml' );
		return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
	}
	
	//关闭连接	
	function __destruct()
	{
		if( !$this->_is_conn() )
		{
			return false;
		}
		@ftp_close( $this->conn_id );
	}

}